﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace LiWi
{
    public class WindowsAdapt
    {


        #region 控件大小随窗体大小等比例缩放
        protected Form form;
        protected float currentWidth;
        protected float currentHeight;
        protected static bool enable = false;
        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
                if (con.Controls.Count 
                    > 0) 
                    setTag(con);
            }
            form.Resize += ResizeChanged;
        }

        public void SetForm(Form f) {
            form = f;
            currentWidth = f.Width;
            currentHeight = f.Height;
            foreach (Control con in f.Controls)
            {
                 setTag(con);
            }
        }

        private void setControls(float newx, float newy, Control cons)
        {
            foreach (Control con in cons.Controls)
                if (con.Tag != null)
                {
                    var mytag = con.Tag.ToString().Split(';');
                    con.Width = Convert.ToInt32(Convert.ToSingle(mytag[0]) * newx); 
                    con.Height = Convert.ToInt32(Convert.ToSingle(mytag[1]) * newy); 
                    con.Left = Convert.ToInt32(Convert.ToSingle(mytag[2]) * newx); 
                    con.Top = Convert.ToInt32(Convert.ToSingle(mytag[3]) * newy); 
                    var currentSize = Convert.ToSingle(mytag[4]) * newy;                   
                    if (currentSize > 0) con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                    con.Focus();
                    if (con.Controls.Count > 0) setControls(newx, newy, con);
                }
        }
        public static void SetResizeEnable(bool b) {
            enable = b;
        }
        /// <summary>
        /// 重置窗体布局
        /// </summary>
        private void ReWinformLayout()
        {
            if(!enable)
                { return; }
            Thread.Sleep(1);
            Task.Factory.StartNew(() => {
                form.Invoke(new Action(() => {
                    var newx = form.Width / currentWidth;
                    var newy = form.Height / currentHeight;
                    SetControlVisable(form,false);
                    foreach (Control item in form.Controls)
                    {
                        setControls(newx, newy, item);
                    }
                    SetControlVisable(form, true);
                }));
            });
            
        }
        private void SetControlVisable(Control c,bool b) {
            foreach (Control con in c.Controls) {
                con.Visible = b;
                if (con.Controls.Count > 0)
                    SetControlVisable(con,b);
            }
        }

        private void ResizeChanged(object sender, EventArgs e) {
            this.ReWinformLayout();
        }
        #endregion
    }
}
